package com.cyzy.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyzy.dto.Result;
import com.cyzy.mapper.CourseCategoryMapper;
import com.cyzy.pojo.CourseCategory;
import com.cyzy.service.CourseCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CourseCategoryServiceImpl implements CourseCategoryService {

    @Autowired
    private CourseCategoryMapper categoryMapper;

    @Override
    public Result getCategoryPage(CourseCategory category) {
        try {
            Page<CourseCategory> page = new Page<>(category.getCurrentPage(), category.getPageSize());
            Page<CourseCategory> resultPage = categoryMapper.selectCategoryPage(page, category);
            return Result.ok().data("page", resultPage);
        } catch (Exception e) {
            return Result.error().message("查询课程分类列表失败");
        }
    }

    @Override
    public Result addCategory(CourseCategory category) {
        try {
            int result = categoryMapper.insert(category);
            return result > 0 ? Result.ok() : Result.error().message("添加课程分类失败");
        } catch (Exception e) {
            return Result.error().message("添加课程分类异常");
        }
    }

    @Override
    public Result updateCategory(CourseCategory category) {
        try {
            int result = categoryMapper.updateById(category);
            return result > 0 ? Result.ok() : Result.error().message("更新课程分类失败");
        } catch (Exception e) {
            return Result.error().message("更新课程分类异常");
        }
    }

    @Override
    public Result deleteCategory(Long categoryId) {
        try {
            int result = categoryMapper.deleteById(categoryId);
            return result > 0 ? Result.ok() : Result.error().message("删除课程分类失败");
        } catch (Exception e) {
            return Result.error().message("删除课程分类异常");
        }
    }

    @Override
    public Result changeCategoryStatus(Long categoryId, String isVisible) {
        try {
            CourseCategory category = new CourseCategory();
            category.setCategoryId(categoryId);
            category.setIsVisible(isVisible);
            int result = categoryMapper.updateById(category);
            return result > 0 ? Result.ok() : Result.error().message("状态更新失败");
        } catch (Exception e) {
            return Result.error().message("状态更新异常");
        }
    }
}